<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" class="login-form">
      <h3 class="title">电商后台管理系统</h3>
      <el-form-item>
        <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="用户名">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
            v-model="loginForm.password"
            type="text"
            auto-complete="off"
            placeholder="密码">
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
            :loading="loading"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import axios from 'axios'
import md5 from 'md5'
import { ElMessage } from 'element-plus'

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: "123"
      },
      loading: false,
      register: false,
    }
  }
  , methods: {
    async handleLogin() {
      console.log("handleLogin");
      // axios.post();
      // 加密密码
      this.loginForm.password = md5(this.loginForm.password);
      // 发送ajax请求完成登录功能
      let {data: ret} = await axios.post('http://localhost:80/admin/user/login', this.loginForm);
      console.log(ret);
      if (ret['code'] == 200) {
        // 将用户名(token)存在sessionStorage中
        window.sessionStorage.setItem("token", ret['data'].username);
        // 登录成功跳转到系统首页
        ElMessage({
          message: '登录成功',
          type: 'success'
        });
        // 使用编程式导航守卫
        this.$router.push({name: 'index'});
      }
    }
  }
}
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: bisque;
  background-size: cover;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
}

.login-form .el-input {
  height: 38px;
}

.login-form .el-input input {
  height: 38px;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.link-type {
  color: #337ab7;
  cursor: pointer;
}

.link-type:hover {
  color: rgb(32, 160, 255);
}
</style>